********************************************************************************
* appendix figure 3: move rates in infutor and census data

********************************************************************************

clear
set more off

* load 10 percent national sample of infutor data
use "/media/data/public/infutor/data/infutor_10pct_national_ALLgeoids.dta", clear


// create variables to sum when we collapse to county level
// mig_inf identifies people who moved in the previous year
gen total_inf = 1
gen mig_inf = (combo_year_current == 2017) & hhnum_last != ""

* compute national move rate
egen total_moves=sum(mig_inf)
egen total_people=sum(total_inf)

gen move_rate=total_moves/total_people
sum move_rate total_moves total_people

// Collapse counts to county level to compare infutor move rate to census rate
collapse (sum) mig_inf total_inf, by(countyfips_current)
rename countyfips_current countyfips

// Merge on census 1-year migration data
merge 1:1 countyfips using county_mig_acs_2017_1year
keep if _merge == 3
drop _merge
order county_name, after(countyfips)

// Merge on census 5-year county chars data
merge 1:1 countyfips using county_chars_acs_2017_5year
keep if _merge == 3
drop _merge
order pct_bach_plus med_hh_inc pct_poverty pct_white, after(county_name)

// Rename some vars to shorten
rename *samecty* *scty*
rename *diffcty* *dcty*
rename *samestate* *sstate*
rename *diffstate* *dstate*

// Compute migration rates for each county
gen infutor_rate=mig_inf/total_inf
gen census_rate=mig/total

// Compute rate of infutor to census rate 
gen infutor_ratio=infutor_rate/census_rate

// Label vars for plots
lab var pct_bach_plus "Percent with bachelor's degree"
lab var med_hh_inc "Median household income"
lab var pct_poverty "Percent below poverty line"
lab var pct_white "Percent white"
lab var infutor_ratio "Infutor move rate/Census move rate"

// Graph the local polynomials for variables of interest
foreach var of varlist pct_bach_plus med_hh_inc pct_poverty pct_white {		

	
		// Create upper and lower bounds for ALL tracts
		sum `var', d
		local vlb = r(p1)
		local vub = r(p99)
		
		sum infutor_ratio, d
		local lb = r(p1)
		local ub = r(p99)
	
		lpoly infutor_ratio `var' if inrange(`var2', `lb', `ub') & (`var' > `vlb' & `var' < `vub'), nosc ///
			graphregion(color(white)) ylabel(0(0.2)1) title("")
	
		}

}




